package capstoneproject.dudiachi.Main;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ListView;
import capstoneproject.dudiachi.Controller.IslandArticleManager;
import capstoneproject.dudiachi.Model.IslandArticle;

public class IslandArticleActivity extends BaseActivity {

	private ArrayList<Bitmap> bitArray;
	private ListView listView;
	private IslandArticleListAdapter adapter;
	private ArrayList<IslandArticle> islandArticleList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 3;
		setContentView(R.layout.activity_list);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		
		IslandArticleManager manager = new IslandArticleManager(getApplicationContext());
		
		String tag = bundle.getString("tag");
		if(tag.equals("search")){
			islandArticleList = (ArrayList<IslandArticle>) intent.getSerializableExtra("list");
		}else{
			String id = bundle.getString("id");
			islandArticleList = manager.searchByIslandArticleType(id);
		}
		
		bitArray = new ArrayList<Bitmap>();
		for(IslandArticle item : islandArticleList){
			try {
				bitArray.add(GetBitmapFromAsset.getBitmapFromAsset(this, "Images/BienDao/" + item.getImage1()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		listView = (ListView) findViewById(R.id.list);
		adapter = new IslandArticleListAdapter(getApplicationContext(), islandArticleList, bitArray); 
		adapter.notifyDataSetChanged();
		listView.setAdapter(adapter);
	}
}
